<template>
	<view>
		<uni-notice-bar show-icon scrollable text="规范网络言行 净化网络环境" :speed="50" />
		<view style="margin: 250rpx 0rpx;text-align: center;"  v-if="list[0]==null">
			<image src="../../static/zanwu.png" class="img3"></image>
		</view>
		<view @longpress="longpress(i)" v-for="i in list">
			<uni-list :border="false">
				<uni-list-chat :title="i.nickName" :avatar="i.avatar!=''?baseUrl+i.avatar:'../../static/yonghu.png'" :time="i.time" @click="go(i)"
					:clickable="true" :note="i.text" class="chat"></uni-list-chat>
			</uni-list>
		</view>
	</view>
</template>

<script>
	import {
		removechathistory,
		removechatlist,
		selectchathistorylist,
		selectchatlist
	} from '../../api/myapi.js'
	import config from '@/config'
	export default {
		data() {
			return {
				baseUrl: config.baseUrl,
				list: [],
				inv:false
			}
		},
		methods: {
			longpress(n) {
				this.$modal.confirm(`是否删除此聊天？
(聊天记录也会被删除)`).then(async (i) => {
					if(i){
						console.log(n);
						let r = await removechatlist({
							userid:uni.getStorageSync("userid"),
							toid:n.userId
						})
						let x = await removechathistory({
							fromuser:uni.getStorageSync("username"),
							touser:n.userName
						})
						console.log(x);
						if (r.data) {
							this.selectlist()
						}
					}
				})
			},
			go(i) {
				this.$tab.navigateTo('/pages/message/message?tousername=' + i.userName + "&userid=" + i.userId +
					"&avatar=" + i.avatar)
			},
			async selectlist(){
				let r = await selectchatlist({
					userid: uni.getStorageSync("userid")
				})
				this.list = r.data
				let list1 = [];
				this.list.map(async (i) => {
					let n = await selectchathistorylist({
						username: uni.getStorageSync("username"),
						tousername: i.userName
					})
					let x = n.data
					if (x.length>0) {
						i.time = x[x.length - 1].createtime
						i.text = x[x.length - 1].text
					}
					list1.push(i)
				})
				this.list = list1
			}
		},
		async onLoad() {
			this.selectlist()
			setInterval(()=>{
				console.log(1);
				this.selectlist()
			},10000)
		}
	}
</script>

<style>
	.uni-list-chat {
		padding: 25rpx 15rpx 18rpx;
		border-bottom: 4rpx solid #dcdcdc;
	}
	.img3 {
		width: 350rpx;
		height: 350rpx;
	}
</style>